//
//  WAMallCenterlistController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/24.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WAMallCenterlistController.h"
#import "WAMallOrderCell.h"
#import "WATransactionDetailController.h"
#import "WAOrderModel.h"
#import "WAMallSendGoodsCell.h"

@interface WAMallCenterlistController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageNum;
@property (strong, nonatomic) NSMutableArray *dataSouces;


@property (assign, nonatomic) NSInteger type;//1:待付款  2:待收货  3:已收货
@end

@implementation WAMallCenterlistController

- (instancetype)initWithType:(NSInteger)type
{
    self = [super init];
    if (self) {
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"WAMallOrderCell" bundle:nil] forCellReuseIdentifier:@"WAMallOrderCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WAMallSendGoodsCell" bundle:nil] forCellReuseIdentifier:@"WAMallSendGoodsCell"];
    @weakify(self)
    self.tableView.mj_header = [MJRefreshLoadHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum = 1;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshMoreFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum ++;
        [self requestData];
    }];
    self.tableView.tableFooterView = [UIView new];
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.view.safe_bottom, 0);

    [WNNotice addObserver:self selector:@selector(reloadAction) name:@"WAReloadOrderListNoticeKey" object:nil];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)requestData{
    [WNBusinessRequest getOrderListWithType:self.type pageNum:self.pageNum Succ:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSInteger total = [dict[@"total"] integerValue];
        NSArray *memberList = [WAOrderModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (self.pageNum == 1) {
            self.dataSouces = [NSMutableArray new];
        }
        [self.dataSouces addObjectsFromArray:memberList];
        if(total <= self.dataSouces.count){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
        if (self.dataSouces.count == 0) {
            self.noDataStr = @"无数据";
        }else{
            [self.tableView reloadData];
        }
        [self.tableView reloadData];
        [self.tableView reloadEmptyDataSet];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        self.noDataStr = mes;
        [self.tableView reloadEmptyDataSet];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [WNTool show:mes];
    }];
}

#pragma mark - UITableView Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type == 2) {
        WAMallSendGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WAMallSendGoodsCell"];
        [cell setModel:self.dataSouces[indexPath.row]];
        return cell;
    }else{
        WAMallOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WAMallOrderCell"];
        [cell setModel:self.dataSouces[indexPath.row]];
        [cell setPayActionBlock:^(WAOrderModel * _Nonnull model) {
            [self payActionWithOrderID:model.ID];
        }];
        cell.goPayBtn.hidden = !(self.type == 0);
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouces.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type == 2) {
        return 180;
    }else{
        return 120;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    WATransactionDetailController *VC = [WATransactionDetailController new];
//    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - DZNEmptyDataSetSource
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self.tableView.mj_header beginRefreshing];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    return [WNTool requestErrorWithSting:self.noDataStr];
}

- (void)payActionWithOrderID:(NSInteger)orderID{
   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *TF = alert.textFields.firstObject;
        [self verificationPayPassword:TF.text orderID:orderID];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入支付密码";
        textField.secureTextEntry = YES;
    }];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)verificationPayPassword:(NSString *)pwd orderID:(NSInteger)orderID{
    [WNTool showLoading];
    [WNBusinessRequest payWithOrderID:orderID payPwd:pwd Succ:^(id  _Nonnull responseObject) {
        [WNTool show:@"支付成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self backAction:nil];
            [WNNotice postNotificationName:@"WAReloadOrderListNoticeKey" object:nil];
            [self.tableView.mj_header beginRefreshing];
        });
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
    
}

- (void)reloadAction{
    if(self.type == 1){
        [self.tableView.mj_header beginRefreshing];
    }
}

@end
